// pages/family/recipes/recipes.js
Page({
  data: {
    memberInfoId: '',
    memberList: [],
    recipes: {},
    currentMember: 0,
    mealIndex: 0,
    swiperList:[]
  },
  onChangeCurrent(e) {
    let that = this;
    if (e.detail.source == 'touch') {
      const {
        recipes,
        memberList
      } = that.data;
      const id = memberList[e.detail.current].memberInfoId;
      //请在这里过滤 recipes,需要根据 period 以及 memberInfoId
      let arr = [recipes.breakfasts, recipes.lunchs, recipes.dinners];
      const arr1 = arr.map(period => {
        let filterP = period;
        if (id!='family'){
           filterP = period.filter(i => {
            return i.memberInfos.find(member => member.memberInfoId == id)
          });
        }
        return filterP;
      })
      this.setData({
        currentMember: e.detail.current,
        swiperList: arr1
      })

      console.log('过滤后的', arr1)
    }
  },
  /**
   * 更换早、午、晚餐
   */
  onSwichRecipe(e) {
    this.setData({
      mealIndex: e.currentTarget.dataset.action=='tap'?e.currentTarget.dataset.index:e.detail.current
    })
  },
  /**
   * 打开食谱详情
   */
  onDetails(e){
    const recipe = JSON.stringify(e.currentTarget.dataset.recipe);
    console.log('recipe', e.currentTarget.dataset.recipe);
    wx.navigateTo({
      url: '/pages/foodmanager/recipe/detail/detail?d_info=' + recipe,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    const data = getCurrentPages()[getCurrentPages().length - 2].data; //获取上一页的data
    const recipes = data.recipes;
    const memberList = data.memberList.slice();
    memberList.unshift({
      memberInfoId:'family',
      nickName:'全部',
      headImgUrl: '/images/family_pic.png'
    })
    this.setData({
      memberList: memberList,
      recipes: recipes,
      swiperList: [recipes.breakfasts, recipes.lunchs, recipes.dinners]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})